package com.zenghus.v2.config;

import com.zenghus.v2.mapper.MapperProxy;
import com.zenghus.v2.mapper.MapperRegistory;
import com.zenghus.v2.sqlsession.SqlSession;

import java.lang.reflect.Proxy;
import java.util.HashMap;
import java.util.Map;

public class Configuration {

    private String mapperPath;
    private MapperRegistory mapperRegistory=new MapperRegistory();

    public Configuration(String mapperPath){
        //register mapper
        try {
            mapperRegistory.build(mapperPath);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public MapperRegistory getMapperRegistory() {
        return mapperRegistory;
    }

    public void setMapperRegistory(MapperRegistory mapperRegistory) {
        this.mapperRegistory = mapperRegistory;
    }

    public <T> Object getMapper(Class<T> clazz, SqlSession sqlsession){
        return Proxy.newProxyInstance(clazz.getClassLoader(),new Class[]{clazz},new MapperProxy(sqlsession));
    }

    public static void main(String[] args) throws Exception {
        String str = "我是{name},我来自{city},今年{age}岁";
        Map<String, String> mapstring = new HashMap<String,String>();
        mapstring.put("name", "小明");
        mapstring.put("age", "15");
        mapstring.put("city", "北京");

        for (Map.Entry<String, String> entry : mapstring.entrySet()) {
            str = str.replace("{" + entry.getKey() + "}", entry.getValue());
        }

        System.out.println("我是小明,我来自北京,今年15岁");
        System.out.println(str);
    }
}
